package DynamicProgramming;

public class _474_OnesandZeroes {
    public int findMaxForm(String[] strs, int m, int n) {
        int len = strs.length;
        int[] ones = new int[len];
        int[] zeros = new int[len];
        for (int i = 0; i < len; i++) {
            ones[i] = count('1', strs[i]);
            zeros[i] = count('0', strs[i]);
        }
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i < len; i++) {
            for (int j = m; j >= zeros[i]; j--) {
                for (int k = n; k >= ones[i]; k--) {
                    dp[j][k] = Math.max(dp[j][k], dp[j - zeros[i]][k - ones[i]] + 1);
                }
            }
        }
        return dp[m][n];
    }

    private int count(char c, String s) {
        int count  =0;
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }
}
